package com.hft.biz.trans.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import com.hft.biz.trans.UrlManager;
import com.hft.biz.Vo.UrlVo;
import com.hft.service.trans.UrlService;
import com.hft.utils.BaseUtils;
import com.hft.utils.NumberUtils;

@Service("urlManager")
public class UrlManagerImpl implements UrlManager {
    private static final Logger      logger = LoggerFactory.getLogger(UrlManagerImpl.class);
    @Autowired
    UrlService          urlService;
    @Autowired
    TransactionTemplate transactionTemplate;

    @Override
    public UrlVo insertUrl(String urlSource) {
        if (!checkUrl(urlSource)) {
            return null;
        }
        UrlVo urlVo = new UrlVo(BaseUtils.addProtocolHeader(urlSource.trim()));
        //查看是已存在记录
        UrlVo urlVoselect = urlService.selectUrl(urlVo);
        if (null == urlVoselect) {
            logger.info("未查询到该url信息，将执行新增操作,url="+urlSource);
            if (urlService.insertAndGetId(urlVo) == 1) {
                urlVo.setUrlShort(NumberUtils.dec2any(String.valueOf(urlVo.getUrlId()), 62));
                if (urlService.updateByPK(urlVo) == 1) {
                    return urlVo;
                }
            }
        }
        logger.info("该url数据库已有记录，返回短url信息,urlSource="+urlVoselect.getUrlSource()+",urlShort="+urlVoselect.getUrlShort());
        return urlVoselect;
    }

    /**
     *TODO 从方法待完善，当前无法正确校验URL格式
     * 验证url是否合法
     * @param url
     * @return
     */
    public static boolean checkUrl(String url) {
        try {
            new URL(BaseUtils.addProtocolHeader(url));
            return true;
        } catch (MalformedURLException e) {
            logger.info("URL校验失败"+url);
            return false;
        }
    }

    /** 
     * @see com.hft.biz.trans.UrlManager#selectAll()
     */
    @Override
    public List<UrlVo> selectAll() {
        return urlService.selectAll();
    }

    /** 
     * @see com.hft.biz.trans.UrlManager#selectByPage(com.hft.biz.vo.UrlVo, int, int)
     */
    @Override
    public List<UrlVo> selectByPage(UrlVo urlVo, int startRow, int num) {
        return urlService.selectByPage(urlVo, startRow, num);
    }

    /** 
     * @see com.hft.biz.trans.UrlManager#selectUrlByShorturl(java.lang.String)
     */
    @Override
    public UrlVo selectUrlByShorturl(String url) {
        UrlVo urlVo = new UrlVo();
        urlVo.setUrlShort(url);
        return urlService.selectUrl(urlVo);
    }

    /** 
     * @see com.hft.biz.trans.UrlManager#selectUrlBySourceurl(java.lang.String)
     */
    @Override
    public UrlVo selectUrlBySourceurl(String url) {
        UrlVo urlVo = new UrlVo();
        urlVo.setUrlSource(url);
        return urlService.selectUrl(urlVo);
    }
    
    public static void main(String[] args) {
        System.out.println(checkUrl("baidu.com"));
        System.out.println(checkUrl("http://baidu.com"));
        System.out.println(checkUrl("baiducom"));
        System.out.println(checkUrl("baidu.com/s"));
        System.out.println(checkUrl("http://www.google.com.hk/search?client=aff-1616dh&q=iso8859-1&newwindow=1&channel=t4"));
        System.out.println(checkUrl("http://blog.csdn.net/xueyong1203/article/details/3975022"));
    }
}
